package service;

import java.util.List;

import dao.BookDao;
import dao.PublishDao;
import util.db.Page;
import util.string.StringUtil;
import entity.Book;
import entity.Publish;

public class PublishService {
	
	PublishDao publishDao = new PublishDao();
	
	public Page<Publish> findAllPublish(int nowpagenum){
		Page<Publish> page = publishDao.findAllPublish(nowpagenum);
		return page;
	}

	public boolean addPublish(String publishName) {
		Publish publish = publishDao.findPublishByName(publishName);
		if (publish == null) {
			publishDao.savePublish(StringUtil.getid(),publishName);
			return true;
		}else {
			return false;
		}
	}

	public boolean editPublish(String id,String publishname) {
		Publish publish = publishDao.findPublishByName(publishname);
		if (publish == null) {
			Publish publish2 = publishDao.findPublishById(id);
			publish2.setPublishname(publishname);
			publishDao.update(publish2);
			return true;
		}
		return false;
	}

	public boolean delPublish(String id) {
		BookDao bookDao = new BookDao();
		List<Book> list= bookDao.findBookByPublishId(id);
		if (list.isEmpty()) {
			publishDao.delPublish(id);
			return true;
		}
		return false;
	}

	public Publish findPublishById(String publishid) {
		Publish publish = publishDao.findPublishById(publishid);
		return publish;
	}

	public List<Publish> findAll() {
		List<Publish> list = publishDao.findAll();
		return list;
	}
	
	
	
	
	
	
}
